package com.xauto.pioneer.appuser.activity.takeCar;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.activity.BaseActivity;
import com.xauto.pioneer.appuser.activity.common.TakePhotoActivity;
import com.xauto.pioneer.appuser.http.BaseLHttpHandler;
import com.xauto.pioneer.appuser.http.Constants;
import com.xauto.pioneer.appuser.http.LHttpLib;
import com.xauto.pioneer.appuser.model.ClientSetting;
import com.xauto.pioneer.appuser.model.Image;
import com.xauto.pioneer.appuser.model.JSONStatus;
import com.xauto.pioneer.appuser.util.ViewUtil;
import com.xauto.pioneer.appuser.view.TopBar;

import net.tsz.afinal.FinalDb;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

import static com.xauto.pioneer.appuser.activity.common.TakePhotoActivity.DO_BIG_IMAGE;
import static com.xauto.pioneer.appuser.activity.common.TakePhotoActivity.UPLOAD_IMG;

/**
 * Created by lsx on 2017-11-28.
 */

public class TakePhotoFeedbackActivity extends BaseActivity {
    @Bind(R.id.takephoto_feedback_topBar)
    TopBar takephoto_feedback_topBar;
    /*@Bind(R.id.webView)
    WebView webView;*/
    //第一张图
    @Bind(R.id.First_image)
    ImageView First_image;
    //第一个拍摄按钮
    @Bind(R.id.tvIdInfoTakePhoto)
    TextView tvIdInfoTakePhoto;
    //第二张图
    @Bind(R.id.Second_image)
    ImageView Second_image;
    //第二个拍摄按钮
    @Bind(R.id.tvIdInfoTakePhoto_two)
    TextView tvIdInfoTakePhoto_two;
    //第三张图
    @Bind(R.id.third_image)
    ImageView third_image;
    //第三个拍摄按钮
    @Bind(R.id.tvIdInfoTakePhoto_third)
    TextView tvIdInfoTakePhoto_third;
    //第四张图
    @Bind(R.id.fourth_image)
    ImageView fourth_image;
    //第四个拍摄按钮
    @Bind(R.id.tvIdInfoTakePhoto_fourth)
    TextView tvIdInfoTakePhoto_fourth;
    @Bind(R.id.take_car_text_s)
    TextView take_car_text_s;
    @Bind(R.id.tv_takephoto_feedback_phonehelp)
    TextView tv_takephoto_feedback_phonehelp;
    @Bind(R.id.CarFeedBack)
    EditText CarFeedBack;
    /** 当前上传照片的服务器路径**/
    private String currentUpdatePath = "";
    public ImageLoader imageLoader;
    //车辆编号
    String carNo = "";
    String longitude = "", latitude = "", address = "";
    //设定拍照对象
    int Witch=0;
    int OnePc=0;
    int TwoPc=0;
    int ThreePc=0;
    int FourPc=0;
    String Pauth1="";
    String Pauth2="";
    String Pauth3="";
    String Pauth4="";
    String telNumber="";

    String localPauth1="";
    String localPauth2="";
    String localPauth3="";
    String localPauth4="";
    //判断什么界面
    String status="0";
    @Override
    protected int getContentView() {
        return R.layout.ac_take_photo_feedback;
    }

    @Override
    protected void initView() {
        initTelNumber();
        take_car_text_s.setClickable(false);
        takephoto_feedback_topBar.showView(TopBar.TITLE, TopBar.LEFT_IMAGE_BTN,TopBar.RIGHT_TV_BTN);
        takephoto_feedback_topBar.setText(TopBar.RIGHT_TV_BTN, "联系客服");
        takephoto_feedback_topBar.setText(TopBar.TITLE, "异常取消订单");
        takephoto_feedback_topBar.setImage(TopBar.LEFT_IMAGE_BTN, R.mipmap.ic_common_left);
        takephoto_feedback_topBar.setLeftOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
//        takephoto_feedback_topBar.setRightOnClick(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+telNumber));
//                startActivity(intent);
//            }
//        });
    }
    private void initTelNumber(){
        FinalDb finalDb = FinalDb.create(mContext);
        List<ClientSetting> client = finalDb.findAll(ClientSetting.class);
        if (client != null && client.size() >0){
            telNumber = client.get(client.size()- 1).getContact_number();
        }
    }
    @Override
    protected void initData() {
        carNo = getIntent().getStringExtra("car_no");
        longitude = getIntent().getStringExtra("longitude");
        latitude = getIntent().getStringExtra("latitude");
        address = getIntent().getStringExtra("address");
        status= getIntent().getStringExtra("status");
    }
    @OnClick({R.id.tv_takephoto_feedback_phonehelp,R.id.tvIdInfoTakePhoto,R.id.tvIdInfoTakePhoto_two,
            R.id.tvIdInfoTakePhoto_third,R.id.tvIdInfoTakePhoto_fourth,
            R.id.take_car_text_s,R.id.First_image,R.id.Second_image,R.id.third_image,R.id.fourth_image})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_takephoto_feedback_phonehelp:
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+telNumber));
                startActivity(intent);
                break;
            case R.id.take_car_text_s:
                    OrderFeedBackInfo(Pauth1,Pauth2,Pauth3,Pauth4);
                break;
            case R.id.First_image:
                if(OnePc ==1){
                    ViewUtil.showBigPic(this,localPauth1);
                }
                break;
            case R.id.Second_image:
                if(TwoPc ==1){
                    ViewUtil.showBigPic(this,localPauth2);
                }
                break;
            case R.id.third_image:
                if(ThreePc ==1){
                    ViewUtil.showBigPic(this,localPauth3);
                }
                break;
            case R.id.fourth_image:
                if(FourPc ==1){
                    ViewUtil.showBigPic(this,localPauth4);
                }
                break;
            case R.id.tvIdInfoTakePhoto:
                startActivityForResult(TakePhotoActivity.getTakePhotoStartIntent(mContext,
                        DO_BIG_IMAGE, 1, 1,1080,1080), UPLOAD_IMG);
                Witch=1;
                break;
            case R.id.tvIdInfoTakePhoto_two:
                startActivityForResult(TakePhotoActivity.getTakePhotoStartIntent(mContext,
                        DO_BIG_IMAGE, 1, 1,1080, 1080), UPLOAD_IMG);
                Witch=2;
                break;
            case R.id.tvIdInfoTakePhoto_third:
                startActivityForResult(TakePhotoActivity.getTakePhotoStartIntent(mContext,
                        DO_BIG_IMAGE, 1, 1,1080, 1080), UPLOAD_IMG);
                Witch=3;
                break;
            case R.id.tvIdInfoTakePhoto_fourth:
                startActivityForResult(TakePhotoActivity.getTakePhotoStartIntent(mContext,
                        DO_BIG_IMAGE, 1, 1,1080,1080), UPLOAD_IMG);
                Witch=4;
                break;
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 101 && resultCode == TakePhotoActivity.UPLOAD_SUCCESS) {
            if (data != null && data.getExtras() != null) {
                currentUpdatePath=data.getExtras().getString("PATH");
                Bitmap bmp= BitmapFactory.decodeFile(data.getExtras().getString("PATH"));
                if(Witch==1){
                    localPauth1 = currentUpdatePath;
                        loadEditImage(currentUpdatePath,Constants.IMAGE_ORDER_FEEDBACK);
                        First_image.setImageBitmap(bmp);
                    tvIdInfoTakePhoto.setText("点击重新拍照");
                    OnePc=1;
                }
                if(Witch==2){
                    localPauth2 = currentUpdatePath;
                        loadEditImage(currentUpdatePath,Constants.IMAGE_ORDER_FEEDBACK);
                        Second_image.setImageBitmap(bmp);
                    tvIdInfoTakePhoto_two.setText("点击重新拍照");
                        TwoPc=1;
                }
                if(Witch==3){
                    localPauth3= currentUpdatePath;
                        loadEditImage(currentUpdatePath,Constants.IMAGE_ORDER_FEEDBACK);
                    third_image.setImageBitmap(bmp);
                    tvIdInfoTakePhoto_third.setText("点击重新拍照");
                         ThreePc=1;
                }
                if(Witch==4){
                        localPauth4 = currentUpdatePath;
                        loadEditImage(currentUpdatePath,Constants.IMAGE_ORDER_FEEDBACK);
                    fourth_image.setImageBitmap(bmp);
                    tvIdInfoTakePhoto_fourth.setText("点击重新拍照");
                        FourPc=1;
                }
                if(OnePc==1||TwoPc==1||ThreePc==1||FourPc==1){
                    take_car_text_s.setClickable(true);
                    ViewUtil.GetViewShgape(take_car_text_s);
                    take_car_text_s.setBackgroundResource(R.drawable.bg_btn_green_selector);
                    ViewUtil.SetViewShgape(take_car_text_s);

                }

            }
        }
    }
    private void loadEditImage(String path,String TYPE) {
        new LHttpLib().editImage(mContext, memberId, TYPE, path, new BaseLHttpHandler(mContext, true, true) {
            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                final Image image = new Image().parse(jsonStatus.data.optJSONObject("image_info"));
                currentUpdatePath = image.source;
                if(Witch==1){
                    Pauth1=image.source;}
                if(Witch==2){
                    Pauth2=image.source;}
                if(Witch==3){
                    Pauth3=image.source;}
                if(Witch==4){
                    Pauth4=image.source;}
            }
        });
    }
    /**
     * 车辆异常反馈
     * */
    private void OrderFeedBackInfo(String Pauth1,String Pauth2,String Pauth3,String Pauth4) {
        JSONObject jsonObject = new JSONObject();
        JSONArray arry=new JSONArray();
        arry.put(Pauth1); arry.put(Pauth2); arry.put(Pauth3); arry.put(Pauth4);
        try {
            jsonObject.put("feedback_comment", CarFeedBack.getText().toString());
            jsonObject.put("car_order_feedback_images", arry);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        new LHttpLib().getOrderFeedbackInfo(mContext, memberId, carNo, jsonObject, new BaseLHttpHandler(mContext, true, true) {
            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                finish();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        FoundCarActivity.Order=1;
                        FoundCarActivity.OrderPhoto=1;
                        finish();
                    }
                });
            }
        });
    }
}
